package com.doudou.oauth.entity;

import com.doudou.client.entity.UserDetailsInfoDto;
import com.doudou.commons.utils.MyAssert;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.management.relation.Role;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 傻男人
 * @Date: 2020/5/21 9:44
 * @Version: 1.0
 * @Description: 认证的user
 */
@ApiModel("认证用户")
@Data
public class AuthUserDto implements UserDetails,Serializable {

    @ApiModelProperty("用户id")
    private Long userId;

    @ApiModelProperty("登录用户名")
    private String username;

    @JsonIgnore
    @ApiModelProperty("用户密码")
    private String password;

    @ApiModelProperty("用户昵称")
    private String nickname;

    @ApiModelProperty("用户头像")
    private String photo;

    @ApiModelProperty("是否启用")
    private Boolean enabled;

    @ApiModelProperty("是否锁定")
    private Boolean locked;

    @ApiModelProperty("角色信息")
    private List<Role> roles;

    public AuthUserDto(){}

    public AuthUserDto(String username, String password, UserDetailsInfoDto userDetailsInfoDto, Collection<? extends GrantedAuthority> authorities) {
        MyAssert.notEmpty(username," username is empty ");
        MyAssert.notEmpty(password," password is empty ");
        this.username = username;
        this.password = password;
        this.enabled = true;
        this.locked = false;
        this.roles = (List)authorities.stream().map((s)->{return s.getAuthority();}).collect(Collectors.toList());
        this.nickname = userDetailsInfoDto.getNikeName();
        this.photo = userDetailsInfoDto.getPhoto();
        this.userId = userDetailsInfoDto.getUserAccountInfoId();
    }

    public AuthUserDto(String username, UserDetailsInfoDto userDetailsInfoDto, Collection<? extends GrantedAuthority> authorities) {
        MyAssert.notEmpty(username," username is empty ");
        this.username = username;
        this.enabled = true;
        this.locked = false;
        this.roles = (List)authorities.stream().map((s)->{return s.getAuthority();}).collect(Collectors.toList());
        this.nickname = userDetailsInfoDto.getNikeName();
        this.photo = userDetailsInfoDto.getPhoto();
        this.userId = userDetailsInfoDto.getUserAccountInfoId();
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
